package com.example.testapplication.ui.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;

import com.example.testapplication.BR;
import com.example.testapplication.R;
import com.example.testapplication.databinding.FragResetpasswordBinding;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class ResetPasswordFragment extends BaseFragment<FragResetpasswordBinding,ResetPsdFragmentViewModel> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.frag_resetpassword;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initViewObservable() {
        viewModel.uc.pClickEvent.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                switch (integer){
                    case 1:
                        NavHostFragment.findNavController(ResetPasswordFragment.this).popBackStack();
                        break;
                    case 2:
                        if(TextUtils.isEmpty(viewModel.password1.get())&& TextUtils.isEmpty(viewModel.password2.get())){
                            binding.etPasswordResetPassword1.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake_anim));
                            binding.etPasswordResetPassword2.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake_anim));
                            ToastUtils.showShort(R.string.common_password_input_error);
                            return;
                        }

                        if (!viewModel.password1.get().equals(viewModel.password2.get())) {
                            binding.etPasswordResetPassword1.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake_anim));
                            binding.etPasswordResetPassword2.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake_anim));
                            ToastUtils.showShort(R.string.common_password_input_unlike);
                            return;
                        }
                        if (true) {
                            NavHostFragment.findNavController(ResetPasswordFragment.this).popBackStack(R.id.login,true);
                            return;
                        }
                        break;
                }
            }
        });
    }
}
